package com.rowline.admin.module.study.dynamicproxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * ClassName:DemoInterceptor
 * Created by 245167
 * on 2018/3/5  05:15
 * description:
 */
public class DemoInterceptor implements MethodInterceptor{

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println("cglib before #####");
        Object object = methodProxy.invokeSuper(o,objects);
        System.out.println("cglib after #####");

        return object;
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(RealSubject.class);
        enhancer.setCallback(new DemoInterceptor());
        Subject subject = (Subject) enhancer.create();
        subject.hello("what are you doing");
        subject.rent();
    }

}
